package com.wnxy.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.portal.entity.Book;
import com.wnxy.portal.mapper.BookMapper;
import com.wnxy.portal.model.vo.PageVo;
import com.wnxy.portal.service.IBookService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jet
 * @since 2023-12-11
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Override
    public List<Book> bookTop10() {
        Wrapper<Book> wrappers =
                Wrappers.lambdaQuery(Book.class)
                        .select(Book::getId,Book::getName,Book::getPrice,Book::getImgsrc,Book::getBuycount)
                        .orderByDesc(Book::getBuycount)
                        .last("limit 10");
        List<Book> list = this.list(wrappers);
        return list;
    }

    @Override
    public PageVo<Book> findBookByType(Integer typeId, Integer pageNum, Integer pageSize) {
        Wrapper<Book> wrapper = Wrappers.lambdaQuery(Book.class)
                .select(Book::getId, Book::getName, Book::getPrice, Book::getImgsrc, Book::getBuycount)
                .eq(typeId != null, Book::getTypeId, typeId);
        Page<Book> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return new PageVo<>(page.getRecords(),page.getTotal());
    }
}
